//留言api
const router = require('koa-router')();
const mysqlApi = require('../lib/mysql');

//添加留言
router.post('/comment/add/:essayId', async (ctx, next) => {
  let data = ctx.request.body
  let essayId = ctx.params.essayId
  let win_comments
  //添加评论
  await mysqlApi.insertComment_table([ctx.session.sessionUser, essayId, data.content])
  // 查找留言length 通过文章id
  await mysqlApi.seletComment_tableEssayId(essayId).then(win => {
    win_comments = win.length
  })
  //更新评论
  await mysqlApi.updateEssay_tableComments([win_comments, essayId]).then((() => {
    ctx.body = {
      code: 200,
      msg: '更新成功'
    }
  })).catch(() => {
    ctx.body = {
      code: 400,
      msg: '失败'
    }
  })

})

//删除评论
router.post('/comment/del', async (ctx, next) => {
  let data = ctx.request.body
  console.log(data);

  await mysqlApi.delComment_tableId(data.ID).then(win => {
    ctx.body = {
      code: 200,
      msg: '删除成功'
    }
  })
})



module.exports = router